﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using OpenSocialPlugin.General;

using OpenSocialPlugin.Providers.Sites.Facebook;


using Facebook.Winforms;
using Facebook.Winforms.Components;
using Facebook.Schema;

namespace OpenSocialPlugin.Providers
{
    public class Facebook : BaseSite, ISocialSite
    {
        private FacebookService facebookService1;
        SiteSettings settings = new SiteSettings();

        public Facebook()
        {
            settingForm = new SettingControl();
            settings.Load();
            settingForm.settings = settings;
            _capabilities = new List<CapabilitiesEnum>();
            _capabilities.Add(CapabilitiesEnum.HasFriend);
            _capabilities.Add(CapabilitiesEnum.HasPhotos);
            _capabilities.Add(CapabilitiesEnum.HasOtherPhotos);
        }


        public FacebookService Service
        {
            get
            {
                return facebookService1;
            }
        }

        #region ISocialSite Members

      public string GetPersonInfo(OSP_Person person)
      {
        //IList<facebookevent> we = Service.Events.Get(Convert.ToInt64(person.Id));
        
        StringBuilder builder = new StringBuilder();
        user us = Service.Users.GetInfo(Convert.ToInt64(person.Id));
        
        if (!string.IsNullOrEmpty(us.name))
          builder.AppendLine("Name :" + us.name);
        if (!string.IsNullOrEmpty(us.birthday_date))
          builder.AppendLine("Birthday :" + us.birthday_date);
        if (us.hometown_location != null)
          builder.AppendLine(string.Format("Hometown :{0}, {1}", us.hometown_location.country, us.hometown_location.city));
        if (us.current_location != null)
          builder.AppendLine(string.Format("Current Location :{0}, {1}", us.current_location.country, us.current_location.city));
        if (!string.IsNullOrEmpty(us.about_me))
          builder.AppendLine("About Me :" + us.about_me);
        if (!string.IsNullOrEmpty(us.religion))
          builder.AppendLine("Religion :" + us.religion);
        if (!string.IsNullOrEmpty(us.relationship_status))
          builder.AppendLine("Relationship :" + us.relationship_status);
        if (!string.IsNullOrEmpty(us.movies))
          builder.AppendLine("Movies :" + us.movies);
        if (!string.IsNullOrEmpty(us.music))
          builder.AppendLine("Music :" + us.music);
        if (!string.IsNullOrEmpty(us.interests))
          builder.AppendLine("Interests :" + us.interests);
        if (!string.IsNullOrEmpty(us.activities))
          builder.AppendLine("Activities :" + us.activities);
        
        return builder.ToString();
      }

      public string GetPhotoInfo(OSP_Photo photo)
      {
        throw new NotImplementedException();
      }

      public List<string> SearchNames { get { return null; } }
        public List<string> ExtrasNames { get { return null; } }

        public GeneralResponse Extras(string serchtxt, int type)
        {
            return new GeneralResponse();
        }

        public GeneralResponse Search(string serchtxt, int type)
        {
            return new GeneralResponse();
        }


        public GeneralResponse Home()
        {
            GeneralResponse res = new GeneralResponse();
            OSP_Person pers = Login();
            List<OSP_Person> list = GetFriends(pers);
            res.Response = list;
            res.Title = string.Format("Friends of {0} ({1})", pers, list.Count);
            return res;
        }

        public List<OSP_Person> GetFriends(OSP_Person person)
        {
          List<OSP_Person> list = new List<OSP_Person>();
          IList<user> users = Service.Friends.GetUserObjects();
          foreach (user u in users)
          {
            list.Add(To_Osd_Person(u));
          }
          //list.Add(GetPerson(Service.uid.ToString()));
          return list;
        }

      public List<OSP_Album> GetAlbums(OSP_Person person)
        {
            List<OSP_Album> list = new List<OSP_Album>();
            IList<album> albums = Service.Photos.GetAlbums(Convert.ToInt64(person.Id), null);
            foreach (album a in albums)
            {
                OSP_Album al = new OSP_Album(a.aid,a.owner.ToString());
                al.Title = a.name;
                list.Add(al);
            }
            return list;
        }

        public List<OSP_Photo> GetPhotos(OSP_Album album)
        {
            List<OSP_Photo> list = new List<OSP_Photo>();
            IList<photo> photos = Service.Photos.Get(null, album.Id, null);
            foreach (photo ph in photos)
            {
                OSP_Photo p = new OSP_Photo();
                p.AlbumId = album.Id;
                p.OwnerId = album.OwnerId;
                p.Title = ph.caption;
                p.Thumb = ph.src;
                p.Url = ph.src_big;
                list.Add(p);
            }
            return list;
        }

        public void Init()
        {
            facebookService1 = new FacebookService();
            facebookService1.ApplicationKey = "c71c3fa5b3c10b40c104e6be84e4ad65";
            facebookService1.Api.Session.ApplicationSecret = "7787db0b49adebc7b4408fc252ecb819";
            settings.Load();
        }

        public OSP_Person Login()
        {
            facebookService1.Api.AuthToken = settings.Token;
            Log.Error(settings.Uid);
            facebookService1.uid = Convert.ToInt64(settings.Uid);
            //facebookService1.Secret = 
            facebookService1.Api.Session.SessionSecret = settings.Secret;
            facebookService1.SessionKey = settings.Session;
            return GetPerson(Service.uid.ToString());
        }

        private SettingControl settingForm;
        public System.Windows.Forms.Control SettingForm
        {
            get
            {
                return settingForm;
            }
        }

        List<CapabilitiesEnum> _capabilities;
        public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }

        public OSP_Person GetPerson(string personId)
        {
            user user = Service.Users.GetInfo(Convert.ToInt64(personId));
            return To_Osd_Person(user);
        }

   


        public override string ToString()
        {
            return "Facebook";
        }

        #endregion

        public virtual OSP_Person To_Osd_Person(user per)
        {
            OSP_Person person = new OSP_Person();
            person.Name = per.name;
            person.Id = per.uid.ToString();
            person.Thumb = per.pic;
            person.LargeThumb = per.pic_big;
            return person;
        }
    }
}
